using Services;
using Services.ObjectPools;
using System.Collections.Generic;
using UnityEngine;

namespace Gobang
{
    public class ChessGenerator : MonoBehaviour
    {
        private IObjectManager objectManager;
        private ChessboardMono chessboard;

        private void AfterChessChange()
        {
            ObjectPoolUtility.RecycleMyObjects(gameObject);
            foreach (KeyValuePair<Vector2Int, int> pair in chessboard.Chessboard.chesses)
            {
                string s = pair.Value switch
                {
                    1 => "Black",
                    2 => "White",
                    _ => string.Empty,
                };
                objectManager.Activate(s, chessboard.ChessboardToWorld(pair.Key), Vector3.zero, transform);
            }
        }

        private void Awake()
        {
            objectManager = ServiceLocator.Get<IObjectManager>();
            chessboard = GetComponentInParent<ChessboardMono>();
        }

        private void OnEnable()
        {
            chessboard.AfterChessChange += AfterChessChange;
        }

        private void OnDisable()
        {
            chessboard.AfterChessChange -= AfterChessChange;
        }
    }
}